USE [master]
GO

IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'ImageCatalog')
DROP DATABASE [ImageCatalog]
GO

CREATE DATABASE [ImageCatalog]
GO

ALTER DATABASE [ImageCatalog] SET COMPATIBILITY_LEVEL = 100
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ImageCatalog].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [ImageCatalog] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [ImageCatalog] SET ANSI_NULLS OFF
GO
ALTER DATABASE [ImageCatalog] SET ANSI_PADDING OFF
GO
ALTER DATABASE [ImageCatalog] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [ImageCatalog] SET ARITHABORT OFF
GO
ALTER DATABASE [ImageCatalog] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [ImageCatalog] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [ImageCatalog] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [ImageCatalog] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [ImageCatalog] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [ImageCatalog] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [ImageCatalog] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [ImageCatalog] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [ImageCatalog] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [ImageCatalog] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [ImageCatalog] SET  DISABLE_BROKER
GO
ALTER DATABASE [ImageCatalog] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [ImageCatalog] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [ImageCatalog] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [ImageCatalog] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [ImageCatalog] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [ImageCatalog] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [ImageCatalog] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [ImageCatalog] SET  READ_WRITE
GO
ALTER DATABASE [ImageCatalog] SET RECOVERY SIMPLE
GO
ALTER DATABASE [ImageCatalog] SET  MULTI_USER
GO
ALTER DATABASE [ImageCatalog] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [ImageCatalog] SET DB_CHAINING OFF
GO

USE [ImageCatalog]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Signatures](
	[ImageID] [int] IDENTITY(1,1) NOT NULL,
	[ImageSignature] [nvarchar](max) NOT NULL,
	[ImageOriginal] [varbinary](max) NOT NULL,
	[ImageBinarized] [varbinary](max) NOT NULL,
	[ImageName] [nvarchar](50) NOT NULL,
	[DistanceToPivot] [float] NULL,
	[PivotID] [int] NULL,
 CONSTRAINT [PK_Signatures] PRIMARY KEY CLUSTERED 
(
	[ImageID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Pivots](
	[PivotID] [int] IDENTITY(1,1) NOT NULL,
	[ImageID] [int] NOT NULL,
 CONSTRAINT [PK_Pivots] PRIMARY KEY CLUSTERED 
(
	[PivotID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Signatures]  WITH CHECK ADD  CONSTRAINT [FK_Signatures_Pivots] FOREIGN KEY([PivotID])
REFERENCES [dbo].[Pivots] ([PivotID])
ON DELETE SET NULL
GO
ALTER TABLE [dbo].[Signatures] CHECK CONSTRAINT [FK_Signatures_Pivots]
GO

ALTER TABLE [dbo].[Pivots]  WITH CHECK ADD  CONSTRAINT [FK_Pivots_Signatures] FOREIGN KEY([ImageID])
REFERENCES [dbo].[Signatures] ([ImageID])
GO
ALTER TABLE [dbo].[Pivots] CHECK CONSTRAINT [FK_Pivots_Signatures]
GO
